//
//  LuaAndXMLViewController.m
//  ForAddSDK
//
//  Created by liboxiang on 2018/1/18.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "LuaAndXMLViewController.h"
#import "DataHandler.h"

@interface LuaAndXMLViewController ()<NSTableViewDataSource,NSTableViewDelegate>

@property (weak) IBOutlet NSTableView *tableView;
@property (weak) IBOutlet NSTextField *gameStartVersionTextField;
@property (weak) IBOutlet NSTextField *chineseNameTextField;
@property (weak) IBOutlet NSTextView *logTextView;
@property (strong, nonatomic) NSArray *productsArray;
@property (strong, nonatomic) NSMutableArray *productIDArray;
@property (weak) IBOutlet NSPathControl *trunkSvnPathControl;

@end

@implementation LuaAndXMLViewController

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do view setup here.
    _tableView.rowHeight = 25;
    _productIDArray = [NSMutableArray arrayWithCapacity:0];
    _productsArray = [NSArray arrayWithObjects:@"6",@"18",@"30",@"98",@"128",@"198",@"328",@"648", nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gameIDChange:) name:SelectedGameIDChangeNotification object:nil];
    [_tableView reloadData];
}

- (void)gameIDChange:(NSNotification *)notification {
    NSString *gameID = notification.object;
    if (![gameID isEqualToString:@"none"]) {
        NSDictionary *dic = [DataHandler getDefaultValuesWithGameID:gameID];
        if (dic) {
            _gameStartVersionTextField.stringValue = dic[COLUMNID_GAME_START_VERSION];
            _trunkSvnPathControl.stringValue = dic[COLUMNID_LOCAL_TRUNK_SVN_PATH];
        }
    }
}
- (IBAction)createAllGameID:(id)sender {
    NSTextField *textField = [_tableView viewAtColumn:1 row:0 makeIfNecessary:NO];
    NSString *productID = DEL_SPACE(textField.stringValue);
    if (textField && [productID containsString:@"6"]) {
        [_productIDArray removeAllObjects];
        [_productIDArray addObject:productID];
        for (int i = 1; i < _productsArray.count; i++) {
            [_productIDArray addObject:[productID stringByReplacingCharactersInRange:[productID rangeOfString:@"6"] withString:_productsArray[i]]];
        }
        [_tableView reloadData];
    }
    else {
        _logTextView.string = @"一键生成失败";
    }
}

- (IBAction)createLuaAndXML:(id)sender {
    DataHandler *handler = [DataHandler shareInstance];
    if (!handler.isPathOK || handler.workPlatform.length == 0) {
        _logTextView.string = @"请回到路径相关页面设置有效路径和平台名";
        return;
    }
    [self createWithSvnPath:handler.svnPath];
}

- (IBAction)createTrunkLuaAndXml:(id)sender {
    DataHandler *handler = [DataHandler shareInstance];
    if (handler.workPlatform.length == 0) {
        _logTextView.string = @"请回到路径相关页面设置有效平台名";
        return;
    }
    if (DEL_SPACE(_trunkSvnPathControl.stringValue).length == 0) {
        _logTextView.string = @"请设置有效trunk的svn路径";
        return;
    }
    [self createWithSvnPath:DEL_SPACE(_trunkSvnPathControl.stringValue)];
}

- (void)createWithSvnPath:(NSString *)svnPath
{
    DataHandler *handler = [DataHandler shareInstance];
    NSInteger count = _tableView.numberOfRows;
    NSMutableArray *products = [NSMutableArray arrayWithCapacity:0];
    for (int i = 0; i < count; i++) {
        NSTextField *textField = (NSTextField *)[_tableView viewAtColumn:1 row:i makeIfNecessary:NO];
        if (textField && DEL_SPACE(textField.stringValue).length) {
            [products addObject:DEL_SPACE(textField.stringValue)];
        }
    }
    
    if (products.count != count) {
        if (products.count > 0) {
            _logTextView.string = @"请完成产品ID的输入";
            return;
        }
        else {
            for (int i = 0; i < count; i++) {
                [products addObject:@""];
            }
        }
    }
    
    NSString *gameStartVersion;
    if (DEL_SPACE(_gameStartVersionTextField.stringValue).length) {
        gameStartVersion = DEL_SPACE(_gameStartVersionTextField.stringValue);
    }
    else {
        _logTextView.string = @"请输入初始游戏版本号";
        return;
    }
    
    NSString *chineseName;
    if (DEL_SPACE(_chineseNameTextField.stringValue).length) {
        chineseName = DEL_SPACE(_chineseNameTextField.stringValue);
    }
    else {
        _logTextView.string = @"请输入platname中的中文名";
        return;
    }
    
    NSMutableArray *arguments = [NSMutableArray arrayWithObject:svnPath];
    [arguments addObjectsFromArray:products];
    [arguments addObject:gameStartVersion];
    [arguments addObject:handler.workPlatform];
    [arguments addObject:chineseName];
    _logTextView.string = [DataHandler runScript:@"createLuaAndXML" type:@"rb" launchPath:@"/usr/bin/ruby" arguments:arguments];
}

- (IBAction)moveProject:(id)sender {
    DataHandler *handler = [DataHandler shareInstance];
    if (!handler.isPathOK || handler.workPlatform.length == 0 || handler.projectSubPath.length == 0 || handler.pictureSubPath.length == 0) {
        _logTextView.string = @"请回到路径相关页面设置有效路径和平台名";
        return;
    }
    if (DEL_SPACE(_trunkSvnPathControl.stringValue).length == 0) {
        _logTextView.string = @"请设置有效trunk的svn路径";
        return;
    }
    NSString *xcodeProjPath = [NSString stringWithFormat:@"%@%@/proj.ios_appstore_%@",handler.svnPath,handler.projectSubPath,handler.workPlatform];
    NSString *trunkProjPath = [NSString stringWithFormat:@"%@%@/proj.ios_appstore_%@",DEL_SPACE(_trunkSvnPathControl.stringValue),handler.projectSubPath,handler.workPlatform];
    NSString *otherPicturePath = [NSString stringWithFormat:@"%@%@/%@",handler.svnPath,handler.pictureSubPath,handler.workPlatform];
    NSString *trunkPicturePath = [NSString stringWithFormat:@"%@%@/%@",DEL_SPACE(_trunkSvnPathControl.stringValue),handler.pictureSubPath,handler.workPlatform];
    
    NSString *projectLog = [DataHandler runScript:@"copyFile" type:@"sh" launchPath:@"/bin/bash" arguments:@[xcodeProjPath,trunkProjPath]];
    NSString *pictureLog = [DataHandler runScript:@"copyFile" type:@"sh" launchPath:@"/bin/bash" arguments:@[otherPicturePath,trunkPicturePath]];
    _logTextView.string = [NSString stringWithFormat:@"%@%@\n完成操作",projectLog, pictureLog];
}

#pragma mark NSTableViewDataSource

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
    return _productsArray.count;
}

#pragma mark NSTableViewDelegate

- (nullable NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(nullable NSTableColumn *)tableColumn row:(NSInteger)row
{
//   productName  productID
    if ([tableColumn.identifier isEqualToString:@"productName"]) {
        NSTextField *textField = [NSTextField new];
        textField.editable = NO;
        textField.stringValue = [NSString stringWithFormat:@"%@00",_productsArray[row]];
        return textField;
    }
    else {
        NSTextField *textField = [NSTextField new];
        if (_productIDArray.count > row) {
            textField.stringValue = _productIDArray[row];
        }
        else {
            textField.stringValue = @"";
        }
        return textField;
    }
}

@end
